<template>
  <el-dialog :title="title" :visible.sync="visible" :close-on-click-modal="false" :close-on-press-escape="false"
    :show-close="false" width="600px">
    <el-alert
      class="mb-30 fs-16"
      :closable="false"
      title="为确保账号安全，请修改密码"
      type="warning"
      show-icon>
    </el-alert>
    <div class="form">
      <el-form ref="form" :model="form" label-width="120px" :rules="rules">
        <el-form-item label="原密码" prop="oldPassword">
          <el-input v-model="form.oldPassword" type="password" placeholder="请输入原密码"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="form.newPassword" type="password" placeholder="请输入新密码"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="form.confirmPassword" type="password" placeholder="请输入确认密码"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { validatePassword } from '@/util/validate'
import { updatePasswordByParams } from "@/api/system/user"
import { getStore } from '@/util/store'
export default {
  data() {
    return {
      visible: true,
      title: '修改密码',
      form: {
        oldPassword: '',
        newPassword: '',
        confirmPassword: ''
      },
      rules: {
        oldPassword: [{
          required: true,
          message: '请输入原密码',
          trigger: 'change'
        }],
        newPassword: [{
          required: true,
          validator: (rule, value, callback) => {
            const validate = validatePassword(value)
            if (validate !== true) {
              callback(new Error(validate));
            } else {
              callback();
            }
          },
          trigger: 'change'
        }],
        confirmPassword: [{
          required: true,
          validator: (rule, value, callback) => {
            if (value === '' || value === undefined) {
              callback(new Error('请确认密码'));
            } else if (value !== this.form.newPassword) {
              callback(new Error('两次输入密码不一致!'));
            } else {
              callback();
            }
          },
          trigger: 'change'
        }]
      }
    }
  },
  
  methods: {
    show() {
      this.visible = true
    },
    submit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const userinfo = getStore({name: 'userInfo'}) || {}
          const params = {
            oldPassword: this.form.oldPassword,
            newPassword: this.form.newPassword,
            newPassword1: this.form.confirmPassword,
            account: userinfo.account
          }
          updatePasswordByParams(params).then(res => {
            if (res.data.success) {
              this.$message({
                type: "success",
                message: "修改密码成功，请重新登录!"
              });
              this.$store.commit('SET_NEED_RESET_PASSWORD', false)
              this.visible = false
              this.$store.dispatch("LogOut").then(() => {
                this.$router.push({
                  path: "/login"
                });
              });
            } else {
              this.$message({
                type: "error",
                message: res.data.msg
              });
            }
          })
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.form {
  width: 90%;
}
</style>
